package com.flower8.service.impl;

import com.flower8.dao.MessageDao;
import com.flower8.dao.UserDao;
import com.flower8.dto.SysMsgDto;
import com.flower8.model.User;
import com.flower8.service.MessageService;
import com.flower8.sysconfig.constants.SysConstants;
import com.flower8.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 消息接口service实现类
 * @Author: tanglin
 * @Date: 2019/12/25 14:48
 * @Version: 1.0
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private UserDao userDao;

    /**
     * 查询消息列表
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public List<SysMsgDto> getMsgList(PageData pd) throws Exception {
        return messageDao.getMsgList(pd);
    }

    /**
     * 查询消息列表数量
     * @param pd
     * @return
     * @throws Exception
     */
    @Override
    public int getMsgListCount(PageData pd) throws Exception {
        return messageDao.getMsgListCount(pd);
    }

    /**
     * 新增一条消息
     * @param param
     * @throws Exception
     */
    @Override
    @Transactional
    public void addMsg(Map param) throws Exception {
        if("2".equals(param.get("type").toString())){
            param.put("icon", SysConstants.MSG2);
            List<User> userAll = userDao.getAllUsers();
            for (User user : userAll){
                param.put("uid",user.getId());
                messageDao.addMsg(param);
            }
        }else{
            param.put("icon", SysConstants.MSG1);
            messageDao.addMsg(param);
        }
    }

    /**
     * 删除消息
     * @param param
     * @throws Exception
     */
    @Override
    @Transactional
    public void delMsg(Map param) throws Exception {
        messageDao.delMsg(param);
    }

    /**
     * 批量删除消息
     * @param ids
     * @throws Exception
     */
    @Override
    @Transactional
    public void delBatchMsg(String ids) throws Exception {
        String idsStr[] = ids.split(",");
        Map map = new HashMap();
        for (String id: idsStr) {
            map.put("id",id);
            messageDao.delMsg(map);
        }
    }
}
